﻿using System;
using System.Collections.Generic;
using System.Text;
using OneMG.Phoenix.Service.Contracts;
using OneMG.Phoenix.Service.Model;
using OneMG.Phoenix.IBatisRepository;
using OneMG.Phoenix.Respository;
using System.Data;
using OneMG.Phoenix.Entity;
using System.Collections;

namespace OneMG.Phoenix.Service
{
    public class ProductService : IProductService
    {
		 IProduct product = new ProductRepository();
         ICategory category = new CategoryRepository();
		 
        public System.Int32 AddProduct(ProductModel model)
        {
			EProduct entity = ConvertModelToEntity(model);
			
            return product.AddProduct(entity);
        }

		public void UpdateByProductID (ProductModel model)
		{
			EProduct entity = ConvertModelToEntity(model);

            product.UpdateByProductID(entity);

		}

		public void DeleteByProductID (System.Int32 productID)
		{
		
            product.DeleteByProductID(productID);

		}
		 
        public ProductModel GetByProductID (System.Int32 productID)
        {
        
			EProduct entity = product.GetByProductID(productID);
			
            ProductModel model = ConvertEntityToModel(entity);


            return model;
        }

        public IList<ProductModel> GetPagedList(int pageSize, int pageIndex,int cateGoryID, out int totalRecord)
        {
            IList<EProduct> entityList = product.GetPagedList(pageSize, pageIndex, cateGoryID, out totalRecord);

            //约定1 为新闻分类
            IList<ECategory> categoryList = category.GetByParentID(2);

            Hashtable ht = new Hashtable();
            foreach (var item in categoryList)
            {
                ht.Add(item.CategoryID, item.CategoryName);
            }

            if (entityList == null)
                return null;

            IList<ProductModel> modelList = new List<ProductModel>();

            foreach (var item in entityList)
            {
                ProductModel model = ConvertEntityToModel(item);
                if (model != null)
                {
                    model.CategoryName = ht[model.CateID].ToString();
                    modelList.Add(model);
                }
            }
            return modelList;


        }




		public ProductModel ConvertEntityToModel(EProduct entity)
		{
			if (entity == null)
            {
                return null;
            }
			ProductModel model = new ProductModel();
                        model.ProductID = entity.ProductID;
                        model.CateID = entity.CateID;
                        model.Title = entity.Title;
                        model.Photo = entity.Photo;
                        model.Description = entity.Description;
                        model.Photographer = entity.Photographer;
                        model.AssistantPhotographer = entity.AssistantPhotographer;
                        model.Stylist = entity.Stylist;
                        model.Model = entity.Model;
                        model.Visitation = entity.Visitation;
                        model.CreateUser = entity.CreateUser;
                        model.CreateTime = entity.CreateTime;
                        model.UpdateUser = entity.UpdateUser;
                        model.UpdateTime = entity.UpdateTime;
                        return model;
		}
		
		public EProduct ConvertModelToEntity(ProductModel model)
		{
			if (model == null)
            {
                return null;
            }
			EProduct entity = new EProduct();
                        entity.ProductID = model.ProductID;
                        entity.CateID = model.CateID;
                        entity.Title = model.Title;
                        entity.Photo = model.Photo;
                        entity.Description = model.Description;
                        entity.Photographer = model.Photographer;
                        entity.AssistantPhotographer = model.AssistantPhotographer;
                        entity.Stylist = model.Stylist;
                        entity.Model = model.Model;
                        entity.Visitation = model.Visitation;
                        entity.CreateUser = model.CreateUser;
                        entity.CreateTime = model.CreateTime;
                        entity.UpdateUser = model.UpdateUser;
                        entity.UpdateTime = model.UpdateTime;
                        return entity;
		}
    }
}